import dayjs from 'dayjs';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD';

/**
 * 到技术
 * @param {*} endTime 
 * @returns 
 */
export function countDown(endTime) {
    const endtime = dayjs(endTime).unix()
    const newtime = dayjs().unix()
    const totalSecond = endtime - newtime

    const d = Math.floor(totalSecond / 60 / 60 / 24)
    const h = Math.floor(totalSecond / 60 / 60 % 24)
    const m = Math.floor(totalSecond / 60 % 60)
    const s = Math.floor(totalSecond % 60)
    return {
        totalSecond, // 剩余总秒数
        d,
        h: h < 10 ? `0${h}` : h,
        m: m < 10 ? `0${m}` : m,
        s: s < 10 ? `0${s}` : s
    }
}

// 格式化日期
export function formatToDateTime(
    date,
    format = DATE_TIME_FORMAT,
) {
    return dayjs(date).format(format);
}